﻿//object helper
(function ($) {
    //push object
    $.fn.pushObject = function (obj) {
        obj = obj || new Object();
        var _setvalue = function (name, val, obj) {
            var names = name.split(".")
            var l = names.length;
            var target = obj;
            for (var i = 0; i < l - 1; i++) {
                if (target[names[i]] == undefined)
                    target[names[i]] = new Object();
                target = target[names[i]];
            }

            target[names[l - 1]] = val;
        };

        var setValue = function (ele, value) {
            var name = ele.attr("name")
            if (name) {
                if (value == null)
                    value = ele.val();

                if (value != null)
                    _setvalue(name, value, obj);
            }
        }

        $(this).find("input").each(function () {
            switch ($(this).attr("type")) {
                case "text":
                case "hidden":
                case "":
                case undefined:
                    setValue($(this));
            }
        }).end().find("input[type='radio']:checked").each(function () {
            setValue($(this));
        }).end().find("select").each(function () {
            setValue($(this));
        }).end().find("textarea").each(function () {
            setValue($(this));
        });

        var checkboxNames = [];
        $(this).find("input:checkbox").each(function () {
            var name = $(this).attr("name");
            if (name && $.inArray(name, checkboxNames) == -1) {
                checkboxNames.push(name);
            }
        });

        var l = checkboxNames.length;
        for (var i = 0; i < l; i++) {
            var checkboxes = $(this).find("input[type='checkbox'][name='" + checkboxNames[i] + "']");
            if (checkboxes.length == 1)
                setValue(checkboxes, checkboxes.is(":checked"));
            else {
                var values = []
                checkboxes.each(function () {
                    if ($(this).is(":checked"))
                        values.push($(this).attr("value"));
                });
                setValue(checkboxes, values);
            }
        }

        return obj;
    };

    //set object
    $.fn.setObject = function (obj) {
        var _getvalue = function (name, obj) {
            var names = name.split(".")
            var l = names.length;
            var target = obj;
            for (var i = 0; i < l; i++) {
                target = target[names[i]];
            }

            return target;
        };

        $(this).find("input[name]").each(function () {
            switch ($(this).attr("type")) {
                case "text":
                case "hidden":
                case "":
                case undefined:
                    $(this).val(_getvalue($(this).attr("name"), obj));
                    break;
                case "radio":
                case "checkbox":
                    var valueAttr = $(this).attr('value');
                    var val = _getvalue($(this).attr("name"), obj);
                    if (val != undefined) {
                        if (typeof val == 'boolean') {
                            if (val != null && val == true)
                                $(this).attr("checked", "checked");
                            else
                                $(this).removeAttr("checked");
                        } else {
                            var l = val.length;
                            var i = 0;
                            for (; i < l; i++) {
                                if ($(this).attr('value') == val[i]) {
                                    $(this).attr("checked", "checked");
                                    break;
                                }
                            }
                            if (i >= l)
                                $(this).removeAttr("checked");
                        }
                    }

                    break;
            }
        }).end().find("select").each(function () {
            $(this).val(_getvalue($(this).attr("name"), obj));
        }).end().find("textarea").each(function () {
            $(this).val(_getvalue($(this).attr("name"), obj));
        });
    };

    //init validation engine class
    $.fn.initValidationEngine = function (rules) {
        generateID = function () {
            var S4 = function () {
                return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
            };
            return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
        }
        var l = rules.length;
        for (var i = 0; i < l; i++) {
            tagName = rules[i][0];
            var tag = $(this).find("*[name='" + tagName + "']");
            if (tag) {
                if (!tag.attr("id"))
                    tag.attr("id", generateID());
                rules[i].splice(0, 1)
                tag.addClass("validate[" + rules[i].join(",") + "]");
            }
        }
    };
})(jQuery);
